package com.whl.sm.service.impl.notice;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.notice.UserCpNoticeMapper;
import com.whl.sm.model.notice.CommunityNotice;
import com.whl.sm.model.notice.UserCpNotice;
import com.whl.sm.model.system.UserCpRole;
import com.whl.sm.service.notice.CommunityNoticeService;
import com.whl.sm.service.notice.UserCpNoticeService;
import com.whl.sm.service.system.UserCpRoleService;
import com.whl.sm.vo.UserCpNoticeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-17
 */
@Slf4j
@Service
public class UserCpNoticeServiceImpl extends ServiceImpl<UserCpNoticeMapper, UserCpNotice> implements UserCpNoticeService {

    @Autowired
    private UserCpRoleService userCpRoleService;

    @Lazy
    @Autowired
    private CommunityNoticeService communityNoticeService;

    @Override
    public Result update(UserCpNotice userCpNotice) {
        try{
            if (Objects.isNull(userCpNotice.getId())){
                getBaseMapper().insert(userCpNotice);
            }else {
                getBaseMapper().updateById(userCpNotice);
            }

        }catch (Exception e){
            log.error("用户公告管理:更新用户公告异常"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result publishDepRole(String noticeId , String roleTypeId) {
        try {
            List<UserCpRole> userList = userCpRoleService.list(new QueryWrapper<UserCpRole>().eq("role_id",roleTypeId));
            userList.stream().forEach(obj->{
                UserCpNotice insertUN = new UserCpNotice(noticeId,obj.getUserId());
                insertUN.setRoleTypeId(roleTypeId);
                update(insertUN);
            });
        }catch (Exception e){
            log.error("个人公告管理--->个人公告发布异常:"+e.getMessage());
        }

        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result publishDepUserList(String noticeId , List<String> userIdList) {
        userIdList.stream().forEach(obj->{
            UserCpNotice insertUN = new UserCpNotice(noticeId,obj);
            update(insertUN);
        });
        return Result.ok();
    }

    @Override
    public Result getByUserId(String userId) {
        List<UserCpNotice> resultList = new ArrayList<>();
        try {
            if (Objects.nonNull(userId)){
                resultList = getBaseMapper().selectList(new QueryWrapper<UserCpNotice>().eq("user_id",userId));
                resultList.stream().forEach(obj->{
                    CommunityNotice linkNotice = communityNoticeService.getById(obj.getNoticeId());
                    obj.setNoticeTitle(linkNotice.getNoticeTitle());
                    obj.setNoticeContent(linkNotice.getNoticeContent());
                });
            }
        }catch (Exception e){
            log.error("获取用户公告消息异常:"+e.getMessage());
            return Result.fail();
        }
        return Result.ok(resultList);
    }

    @Override
    public Result hasReadOperate(String id) {
        try{
            boolean result =  update(new UpdateWrapper<UserCpNotice>()
                    .eq("id",id)
                    .set("read_status",1)
                    .set("read_time",new Date())
            );

        }catch (Exception e) {
            log.error("用户已读操作失败:"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }
}
